package com.shure.surdes.modules.survey.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shure.surdes.common.utils.CloneUtil;
import com.shure.surdes.modules.survey.domain.Options;
import com.shure.surdes.modules.survey.domain.Question;
import com.shure.surdes.modules.survey.domain.Survey;
import com.shure.surdes.modules.survey.mapper.SurveyMapper;
import com.shure.surdes.modules.survey.query.SurveyQuery;
import com.shure.surdes.modules.survey.service.IAnswerJsonService;
import com.shure.surdes.modules.survey.service.IOptionsService;
import com.shure.surdes.modules.survey.service.IQuestionService;
import com.shure.surdes.modules.survey.service.ISurveyService;
import com.shure.surdes.modules.survey.vo.SurveyVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 问卷Service业务层处理
 *
 * @author Shure
 * @date 2021-10-18
 */
@Service
public class SurveyServiceImpl extends ServiceImpl<SurveyMapper, Survey> implements ISurveyService {

    @Autowired
    private SurveyMapper surveyMapper;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IAnswerJsonService answerJsonService;
    @Autowired
    private IOptionsService optionsService;

    /**
     * 批量永久删除问卷
     *
     * @param surveyIds 需要永久删除的问卷主键
     * @return 结果
     */
    @Override
    public int deleteSurveyBySurveyIds(Long[] surveyIds) {
        questionService.deleteQuestionBySurveyIds(surveyIds);
        answerJsonService.deleteAnswerJsonBySurveyIds(surveyIds);
        //answerService.deleteAnswerBySurveyIds(surveyIds);
        return surveyMapper.deleteSurveyBySurveyIds(surveyIds);
    }

    /**
     * 永久删除问卷信息
     *
     * @param surveyId 问卷主键
     * @return 结果
     */
    @Override
    public int deleteSurveyBySurveyId(Long surveyId) {
        return surveyMapper.deleteSurveyBySurveyId(surveyId);
    }

    /**
     * 批量删除问卷
     *
     * @param surveyIds 需要删除的问卷主键
     * @return 结果
     */
    @Override
    public int removeSurveyBySurveyIds(Long[] surveyIds) {
        return surveyMapper.removeSurveyBySurveyIds(surveyIds);
    }

    /**
     * 删除问卷信息
     *
     * @param surveyId 问卷主键
     * @return 结果
     */
    @Override
    public int removeSurveyBySurveyId(Long surveyId) {
        return surveyMapper.removeSurveyBySurveyId(surveyId);
    }

    /**
     * 批量发布问卷
     *
     * @param surveyIds 需要发布的问卷主键集合
     * @return 结果
     */
    @Override
    public int publishSurveyBySurveyIds(Long[] surveyIds) {
        return surveyMapper.publishSurveyBySurveyIds(surveyIds);
    }

    /**
     * 批量还原问卷
     *
     * @param surveyIds 需要发布的问卷主键集合
     * @return 结果
     */
    @Override
    public int restoreSurveyBySurveyIds(Long[] surveyIds) {
        return surveyMapper.restoreSurveyBySurveyIds(surveyIds);
    }

    /**
     * 撤销发布问卷
     *
     * @param surveyId 需要发布的问卷主键
     * @return 结果
     */
    @Override
    public int revokeSurveyBySurveyId(Long surveyId) {
        return surveyMapper.revokeSurveyBySurveyId(surveyId);
    }

    /**
     * 复制问卷
     *
     * @param surveyId
     * @return
     */
    @Override
    @Transactional
    public Long copySurveyBySurveyId(Long surveyId) {
        Survey survey = this.getById(surveyId);
        if (survey != null) {
            survey.setSurveyName(survey.getSurveyName() + " 副本");
            return this.copySurvey(survey);
        }
        return 0L;
    }

    @Override
    public Long saveByTemplate(Survey survey) {
        survey.setIsTemplate("0");
        return this.copySurvey(survey);
    }

    private Long copySurvey(Survey survey){
        Survey newSurvey = CloneUtil.clone(survey, Survey.class);
        Long newSurveyId = IdUtil.getSnowflakeNextId();
        newSurvey.setSurveyId(newSurveyId);
        newSurvey.setCreateTime(null);
        newSurvey.setUpdateTime(null);
        this.save(newSurvey);

        List<Question> questions = questionService.selectQuestionListBySurveyId(survey.getSurveyId());
        List<Options> options = new ArrayList<>();
        for (Question question : questions) {
            Long questionId = question.getQuestionId();
            Long newQuestionId = IdUtil.getSnowflakeNextId();
            question.setSurveyId(newSurveyId);
            question.setQuestionId(newQuestionId);

            List<Options> opList = optionsService.list(new LambdaQueryWrapper<Options>().eq(Options::getQuestionId, questionId));
            for (Options o : opList) {
                o.setQuestionId(newQuestionId);
                o.setOptionId(IdUtil.getSnowflakeNextId());
                options.add(o);
            }
        }
        questionService.saveBatch(questions);
        optionsService.saveBatch(options);
        return newSurveyId;
    }

    @Override
    public Page<SurveyVo> analysisList(Page page, SurveyQuery query) {
        return surveyMapper.analysisList(page, query);
    }

    
}
